package com.cy.game

import android.graphics.Color
import android.widget.FrameLayout
import android.widget.TextView
import com.helloandroid.R
import com.helloandroid.tools.MyLog
import dai.ui.call
import dai.ui.delay
import dai.ui.moveBy
import dai.ui.scaleTo
import dai.ui.single.Call
import dai.ui.single.MoveBy

enum class ItemState(val resId: Int) {
    InvalidBg(0),
    DefaultContentBg(R.drawable.yh_wordnode_yellow), // 默认的内容状态
    WaitFillBg(0),
    DefaultSelRegionBg(R.drawable.yh_wordnode_white),
    ContentFillBg(R.drawable.yh_wordnode_white), // 填入
    FillRight(R.drawable.yh_wordnode_green), // 填写正确
}

class Item(
        val gameFragment: CyGameFragment,
        val xIdx: Int = -1,
        val yIdx: Int = -1,
        var enable: Boolean = false, // 是否启用
        var value: String = "",
        var frameLayout: FrameLayout? = null,
        var textView: TextView? = null,
) {

    var state: ItemState = ItemState.InvalidBg
        set(value) {
            if (field != value) {
                field = value
                textView!!.setBackgroundResource(value.resId)
            }
        }


    private fun contentClicked() {
        val curItem = gameFragment.getCurItem()
        val curValue = curItem.value

        if (curItem.xIdx != xIdx ||
                curItem.yIdx != yIdx) {
            // 里面如果有字 就不是空的
            if (curValue == "") {
                curItem.state = ItemState.WaitFillBg
            } else {
                curItem.state = ItemState.ContentFillBg
            }

            // 如果里面有字,就放到下面
            if (value != "") {
                putWordDown()
            }
            gameFragment.setCurItem(this)
        } else {
            if (curValue != "") {
                curItem.putWordDown()
            }
        }
    }

    fun registClickEvent() {
        frameLayout!!.setOnClickListener {
            MyLog.info("内容区域 clicked")
            contentClicked()
        }
    }

    private fun selRegionClicked() {
        // 填充
        val board = gameFragment.board
        val curItem = gameFragment.getCurItem()

        // curItem 里面如果有字,则置换下来
        if (curItem.value != "") {
            // 把里面的字放下去
            curItem.putWordDown()
        }

        putWordUpTo(curItem)

        //
        var fillWrong = false
        // 1. 填入的词是否已经完成
        val maybeProverbs = board.findRelatedProverbs(curItem)
        if (maybeProverbs.size == 2) {
            // case1:两个都填满
            // case2:都没填满
            // case3:一个填满一个没有填满
            val p0 = maybeProverbs[0]
            val p1 = maybeProverbs[1]
            val p0Filled = p0.isFill(board)
            val p1Filled = p1.isFill(board)
            if (p0Filled && p1Filled) {
                // 可能两个都对
                // 可能两个都错
                // 不存在一对一错的情况
                if (p0.isRightFill(board)) {
                    // 都对
                    // 顺序播动画
                    p0.playRightAnim(board, 0f)
                    p1.playRightAnim(board, 0.4f)
                } else {
                    // 都错
                    p0.playWrongAnim(board)
                    p1.playWrongAnim(board)
                    fillWrong = true
                }
            } else {
                if (p0Filled) {
                    if (p0.isRightFill(board)) {
                        p0.playRightAnim(board, 0f)
                    } else {
                        p0.playWrongAnim(board)
                        fillWrong = true
                    }
                } else if (p1Filled) {
                    if (p1.isRightFill(board)) {
                        p1.playRightAnim(board, 0f)
                    } else {
                        p1.playWrongAnim(board)
                        fillWrong = true
                    }
                }
            }
        } else {
            val p = maybeProverbs[0]
            if (p.isRightFill(board)) {
                // 播放正确动画
                p.playRightAnim(board, 0f)
            } else if (p.isFill(board)) {
                // 播放错误动画
                p.playWrongAnim(board)
                fillWrong = true
            }
        }

        // 发现填错了
        // 1. 不更新 curItem
        // 2. 文字变红
        if (fillWrong) {
            //
            curItem.textView!!.setTextColor(Color.RED)
        } else {
            if (board.checkWin()) {
                MyLog.info("============= 游戏过关!")
                // 延迟个时间,弹出下一关
                gameFragment.cyAct.model.delayRun(0.5f) {
                    // 显示结果弹框
                    gameFragment.showWinDialog()
                }
            } else {
                gameFragment.updateCurItem(curItem)
            }
        }
    }

    fun registSelRegionClickEvent() {
        frameLayout!!.setOnClickListener {
            MyLog.info("选择区域  clicked!")
            selRegionClicked()
        }
    }

    fun playConfirmAnim(dt: Float) {
        // 字变白,底也变,并且不可以点击
        Call { frameLayout!!.isClickable = false }
                .delay(dt)
                .call {
                    textView!!.setTextColor(Color.WHITE)
                    state = ItemState.FillRight
                }.scaleTo(kAnimTime, 1.2f, 1.2f)
                .scaleTo(kAnimTime, 1f, 1f)
                .runBy(frameLayout!!, gameFragment.tweenModel)
    }

    fun playErrorAnim() {
        MoveBy(0.05f, 10f, 0f)
                .moveBy(kWroingAnimTime, -10f, 0f)
                .moveBy(kWroingAnimTime, 10f, 0f)
                .moveBy(kWroingAnimTime, -10f, 0f)
                .moveBy(kWroingAnimTime, 0f, 0f)
                .runBy(frameLayout!!, gameFragment.tweenModel)
    }

    fun getComposeIdx() = xIdx * Board.ContentColumn + yIdx

    fun putWordDown(){
        val selItem = gameFragment.board.findFirstEmptySelItem()
        selItem.value = value
        selItem.textView!!.text = selItem.value
        selItem.state = ItemState.DefaultSelRegionBg
        selItem.frameLayout!!.isClickable = true

        value = ""
        textView!!.text = ""
        textView!!.setTextColor(Color.BLACK)
        state = ItemState.WaitFillBg
    }

    fun putWordUpTo(item:Item){
        item.value = value
        item.textView!!.text = value
        item.state = ItemState.ContentFillBg

        // 当前至空,并且不可点击
        value = ""
        textView!!.text = ""
        state = ItemState.InvalidBg
        frameLayout!!.isClickable = false
    }

    companion object {
        const val kAnimTime = 0.02f
        const val kWroingAnimTime = 0.05f
    }

}